package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import beans.User;
import dao.UserDao;
import utils.DbUtils;

public class UserDaoImplByJDBC implements UserDao{

	@Override
	public void save(User user) {
		String sql="insert into tb_user(uid,password,zombieDieMaxAmount) values(?,?,?)";
		DbUtils.update(sql, new Object[] {user.getUid(),user.getPassword(),user.getZombieDieMaxAmount()});
	}

	@Override
	public User queryByUidAndLoginPwd(String uid, String password) {
		String sql="select * from tb_user where uid=? and password=?";
		User user=null;
		Connection con=DbUtils.getConnection();
		PreparedStatement ps=null;
		ResultSet rs=null;//结果集
		con=DbUtils.getConnection();
		if(con!=null) {
			try {
				ps=con.prepareStatement(sql);
				//要给对应的问号赋值
				ps.setString(1, uid);//第一个问号放用户名
				ps.setString(2, password);//第二个问号放密码
				//执行查询并返回一个结果集对象
				rs=ps.executeQuery();
				if(rs.next()) {
					user = new User();
					user.setUid(rs.getString(1));
					user.setPassword(rs.getString(2));
					user.setZombieDieMaxAmount(rs.getInt(3));
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				//释放资源
				DbUtils.close(con, ps,rs);
			}
		}
		return user;
	}

	@Override
	public void update(User user) { //更新密码和僵尸击杀数量 在uid里在位置
		String sql="update tb_user set password=?,zombieDieMaxAmount=? where uid=?";
		DbUtils.update(sql, new Object[] {user.getPassword(),user.getZombieDieMaxAmount(),user.getUid()});//三个问号
	}

	@Override
	public void deleteByUid(String uid) {
		// TODO Auto-generated method stub
		String sql="delete from tb_user where uid=?";
		DbUtils.update(sql, new Object[] {uid});//?
	}

	@Override
	public List<User> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> queryForPage(int page, int pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

}
